package chapter8.homework;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CountElements {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("apple", "banana", "apple", "orange", "banana", "apple");
        Map<String, Integer> countMap = new HashMap<>();
        // 遍历列表，统计每个元素的出现次数
        for (String item : list) {
            countMap.put(item, countMap.getOrDefault(item, 0) + 1);
        }
        // 输出结果
        for (Map.Entry<String, Integer> entry : countMap.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}